﻿using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
using System.Management.Automation;
using PowerOBIProvider;
using PowerOBIProvider.OBI;
using PowerOBIPRovider.OBIWebReference;
using PowerOBIProvider.Cmdlts;

namespace PowerOBIPRovider.Cmdlts
{
    [Cmdlet("Migrate", "Item",
    SupportsShouldProcess = true, ConfirmImpact = ConfirmImpact.Medium)]
    public class MigrateItemCmdlt : BaseOBICmdlt
    {
        private string source = null;
        [Parameter(Mandatory = true, Position = 1)]
        [ValidateNotNullOrEmpty]
        public string Source
        {
            get
            {
                return source;
            }
            set
            {
                source = value;
            }
        }
        private string target = null;
        [Parameter(Mandatory = true, Position = 2)]
        [ValidateNotNullOrEmpty]
        public string Target
        {
            get
            {
                return target;
            }
            set
            {
                target = value;
            }
        }
        bool force = false;
        [Parameter(Mandatory = false, Position = 3)]
        public SwitchParameter Force
        {
            get
            {
                return force;
            }
            set
            {
                force = value;
            }

        }
        protected override void ProcessRecord()
        {
            PathInfo sourcePathInfo = GetPathInfoFromPath(source);
            PSOBIDriveInfo sourceDrive = GetOBIDrive(sourcePathInfo);
            string rootFolder = target.Split('/')[0];
            if ( this.ShouldProcess(source,
                "migrate a  WebCatalog object") )
            {
                try
                {
                    PathInfo targetPathInfo = GetPathInfoFromPath(target);
                    if (targetPathInfo == null) // path not exist on target need to take root
                    {
                        if (force == false)
                            throw new Exception("Destination does not exist - please use -force switch to override.");
                        targetPathInfo = GetPathInfoFromPath(rootFolder);
                    }
                    PSOBIDriveInfo targetDrive = GetOBIDrive(targetPathInfo);
                    if (sourceDrive != null && targetDrive != null)
                    {

                        ItemInfo sourceItemInfo = sourceDrive.ObiWSClient.GetItemInfo(sourcePathInfo.ProviderPath);
                        if (sourceItemInfo.type == ItemInfoType.Object)
                        {
                            string parentPath = target.Replace(sourceItemInfo.caption, "").TrimEnd('/').Replace(rootFolder, "");
                            if (targetDrive.ObiWSClient.GetItemInfo(parentPath) == null)
                            {
                                targetDrive.ObiWSClient.CreateFolder(parentPath);
                                targetPathInfo = GetPathInfoFromPath(target.Replace(sourceItemInfo.caption, "").TrimEnd('/'));
                            }
                           


                            CatalogObject catObject = sourceDrive.ObiWSClient.ReadItem(sourcePathInfo.ProviderPath);
                            targetDrive.ObiWSClient.WriteItem(catObject, targetPathInfo.ProviderPath + "/" + sourceItemInfo.caption);
                        }
                        else
                            throw new Exception("Only migration of specified objects is currently supported");

                    }

                }
                catch (Exception ex)
                {
                    WriteError(new ErrorRecord(ex, "0", ErrorCategory.OperationStopped, null));
                }
            }
        }
    }
}
